/* eslint-disable */
import axios from 'axios';
axios.defaults.timeout = 5000;  //超时时间设置
axios.defaults.withCredentials = true;  //true允许跨域
axios.defaults.headers['Content-Type'] = 'application/json;charset=UTF-8';
axios.defaults.baseURL = 'http://localhost:8888/api';

// 响应拦截器
axios.interceptors.response.use(response => interceptorResponse(response), error => interceptorError(error));
// 拦截response
export function interceptorResponse(response){
  console.log("interceptorResponse: "+ JSON.stringify(response.data))
  switch (response.status) {
    case 200: return Promise.resolve(response.data) //返回data{state,msg,data}
    default: return Promise.reject(response)
  }
}
// 拦截error
export function interceptorError(error){
  console.log("interceptorError: "+ JSON.stringify(error))
  if (error.response.status) {
    switch (error.response.status) {
      case 401:
        router.replace({ path: '/', query: { redirect: router.currentRoute.fullPath }})
        break
      case 403:
        setTimeout(() => {
          router.replace({ path: '/', query: { redirect: router.currentRoute.fullPath }});
        }, 1000)
        break
      case 404:
        console.log('请求页面飞到火星去了')
        break
    }
    return Promise.reject(error.response)
  }
}
export function get(url, params) {
  console.log("get:")
  console.log("url:" + url)
  console.log("params:" + params)
    return new Promise((resolve, reject) => {
        axios.get(url, {params: params}).then(response => {
                resolve(response);
            }).catch(err => { reject(err) })
    })
}

export function post(url, data) {
  console.log("post:")
  console.log("url:" + url)
  console.log("data:" + JSON.stringify(data))
    return new Promise((resolve, reject) => {
        axios.post(url, data).then(response => {
                resolve(response);
            }, err => { reject(err) })
    })
}
